package com.mulesoft.domain.analyzer;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mulesoft.domain.analyzer.structure.GraphList;
import com.mulesoft.domain.analyzer.structure.Pair;

public class WaysByStepsCalculator {
	
		private GraphList graph;
		
		public WaysByStepsCalculator(String input) {
			this.graph = new GraphList(input);
		}
	
		public Set<String> calculateWays(char current, char destination, int length) {
			Set<String> sol= new HashSet<String>();
			if (length == 0 && current == destination) {
				sol.add(""+destination);
				return sol;
			} 
			if (length == 0 && current != destination) {
				return sol;
			}
			// length > 0
			List<Pair<Character, Integer>> neighbors = graph.getNeighbors(current);
			for (Pair<Character, Integer> neighbor : neighbors) {
				Set<String> preWays = calculateWays(neighbor.getFirst(), destination, length - 1);
				if (preWays.isEmpty()) {
					continue;
				}
				// Ways were found.
				sol.addAll(prefix(current,preWays));
			}
			return sol;
		}
		
		private Set<String> prefix(char current, Set<String> preWays) {
			Set<String> withPrefix = new HashSet<String>();
			for (String preWay : preWays) {
				withPrefix.add(current + "," +preWay);
			}
			return withPrefix;
		}

}
